/*
 * Created on Jan 11, 2011 by Eng. Julian Rousselot
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package ar.com.visualAORE.util;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;

import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.LayeredHighlighter;
import javax.swing.text.Position;
import javax.swing.text.View;

/**
 * @author julian
 *
 */
//Painter for underlined highlights
public class UnderlineHighlightPainter extends LayeredHighlighter.LayerPainter {
  public UnderlineHighlightPainter(Color c) {
    color = c;
  }

  public void paint(Graphics g, int offs0, int offs1, Shape bounds,
      JTextComponent c) {
    // Do nothing: this method will never be called
  }

  public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds,
      JTextComponent c, View view) {
    g.setColor(color == null ? c.getSelectionColor() : color);

    Rectangle alloc = null;
    if (offs0 == view.getStartOffset() && offs1 == view.getEndOffset()) {
      if (bounds instanceof Rectangle) {
        alloc = (Rectangle) bounds;
      } else {
        alloc = bounds.getBounds();
      }
    } else {
      try {
        Shape shape = view.modelToView(offs0,
            Position.Bias.Forward, offs1,
            Position.Bias.Backward, bounds);
        alloc = (shape instanceof Rectangle) ? (Rectangle) shape
            : shape.getBounds();
      } catch (BadLocationException e) {
        return null;
      }
    }

    FontMetrics fm = c.getFontMetrics(c.getFont());
    int baseline = alloc.y + alloc.height - fm.getDescent() + 1;
    g.drawLine(alloc.x, baseline, alloc.x + alloc.width, baseline);
    g.drawLine(alloc.x, baseline + 1, alloc.x + alloc.width,
        baseline + 1);

    return alloc;
  }

  protected Color color; // The color for the underline
}
